package com.xiezhr.pbideaapitestdemo.controller;

import com.xiezhr.pbideaapitestdemo.entity.ApiResponse;
import com.xiezhr.pbideaapitestdemo.entity.Student;
import com.xiezhr.pbideaapitestdemo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: pbideaApiTestDemo
 * @description:
 * @author: xie.zhr
 * @create: 2024-12-07 11:26
 **/
@RestController
@RequestMapping("/api")
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 获取所有学生信息
     * @return
     */
    @GetMapping("/students")
    public ApiResponse<List<Student>> getAllStudents() {
        List<Student> students = studentService.getAllStudents();
        return new ApiResponse<>(200, "获取学生信息成功", students);
    }

    @PostMapping("/stus")
    public List<Student> getAllStus() {
        List<Student> students = studentService.getAllStudents();
        return students;
    }

    /**
     * 根据学生ID获取学生信息
     * @param id
     * @return
     */
    @GetMapping("/students/id/{id}")
    public ApiResponse<Student> getStudentById(@PathVariable String id) {
        Student student = studentService.getStudentById(id);
        if (student != null) {
            return new ApiResponse<>(200, "获取学生信息成功", student);
        } else {
            return new ApiResponse<>(404, "学生未找到", null);
        }
    }

    /**
     * 添加学生信息
     * @param student
     * @return
     */
    @PostMapping("/addstudent")
    public ApiResponse<Student> addStudent(@RequestBody Student student) {

        return studentService.addStudent(student);
    }

    @PostMapping("/addstu")
    public ApiResponse<Student> addStu( Student student) {

        return studentService.addStudent(student);
    }


    /**
     * 获取用户信息
     * @param name
     * @param sex
     * @param age
     * @return
     */
    @GetMapping("/getUser")
    public ApiResponse<Map<String, Object>> getUserInfo(String  name, String sex, int age){
        HashMap<String, Object> map = new HashMap<>();
        map.put("name",name);
        map.put("sex",sex);
        map.put("age",age);

        return new ApiResponse<>(200, "获取学生信息成功", map);
    }
}
